﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Text;

namespace Dune2k1
{
    public class R16
    {
        #region Variables
        byte[] raw;
        Texture2D texture;
        #endregion

        #region Properties
        public Texture2D Texture
        {
            get { return texture; }
        }
        #endregion

        public R16(byte[] raw, GraphicsDevice device)
        {
            this.raw = raw;

            MakeTexture(device);
        }

        private void MakeTexture(GraphicsDevice device)
        {
            texture = new Texture2D(device, 32, 32);

            Color[] temp = new Color[1024];
            UInt16 colour;
            int count = 0;
            for (int i = 29; i < 2077; i+=2)
            {
                colour = BitConverter.ToUInt16(raw, i);

                //Extract colour information from int 16
                //bits = XRRRRRGGGGGBBBBB
                byte red = (byte)((colour & 0x7C00) >> 10);
                byte green = (byte)((colour & 0x3E0) >> 5);
                byte blue = (byte)(colour & 0x1f);

                //Maximum value that can be held by 5 bits is 31, so divide by 31 to get the colour percentage
                temp[count] = new Color((float)red / 31, (float)green / 31, (float)blue / 31);
                count++;
            }

            texture.SetData<Color>(temp);
        }
    }
}
